package com.data.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 命令行工具
 */
@Slf4j
public class CmdUtils {
    private static Logger logger = LoggerFactory.getLogger(CmdUtils.class);

    public static String execCurl(String[] cmds) {

        ProcessBuilder process = new ProcessBuilder(cmds);
        process.redirectErrorStream(true);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            reader.close();

            return builder.toString();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return null;

    }

    public static String execCurlList(List cmds) {

        ProcessBuilder process = new ProcessBuilder(cmds);
        process.redirectErrorStream(true);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            reader.close();

            return builder.toString();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return null;

    }


    /**
     * 签名-加密
     * @param key
     * @param message
     * @return
     * @throws Exception
     */
    public static String HMACMD5Encode(String key, String message) throws NoSuchAlgorithmException, InvalidKeyException {

        SecretKeySpec keySpec = new SecretKeySpec (key.getBytes (),"HmacMD5");

        Mac mac = Mac.getInstance ("HmacMD5");
        mac.init(keySpec);
        byte[] rawHmac = mac.doFinal (message.getBytes ());
        return Hex.encodeHexString (rawHmac);
    }
    public static void main(String[] args){
        //必须分开写，不能有空格
        String[] cmds={"curl"
                ,"-s"
                ,"https://zl-gateway.junrunrenli.com/jrit-zl-biz/bizResume/resumeIsExist?phone=13012168588"
                ,"-H","authority: zl-gateway.junrunrenli.com"
                ,"-H","accept: application/json, text/plain, */*"
                ,"-H","accept-language: zh-CN,zh;q=0.9,en;q=0.8"
                ,"-H","authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImNiZGZiZmVmLWMzZjEtNDg0NS1hYWQ4LWMxZDkwYTI5YWFhMiJ9.eyJwcm9kdWN0IjoiaHJzYy1wcm8iLCJzdWIiOiJ6aGFuZ2ZlaSIsImF1ZCI6IlNTTyIsImV4cCI6MTY1NDc4NjMzNSwiaWF0IjoxNjU0NzQzMTM1LCJpc3MiOiJaTCJ9.Ix5sbaLrx2RDMaJuhSveF_jGOfnmZOitzwuzpYOla1YeNhM3Mf6dTteiovqa7TSr1eR_F6KrQbdgcrNsx045zpG9yVY8joa27Ry0YO81OVj-xKmMIrhqkNF09CogWnaOB9ruKAoM9DvDWdtP8JH-yYbn-eLt8E14zO8rAOTele3h-tSmNrk38q7JHjEF_kyzaRDQCsrPyCthCgHeHhL_pctk-wOmqtnxqC437_q-f-Eflmk-VAHH0t0rCegZXr606zWm7lddUj7o2rxwjfKviv3Nccix0Oi0wpgR1DJscNHC8a0fNqPWALAOesqJpEmDaihykxijeOKA4ZAC9R4t2A"
                ,"-H","origin: https://rpo2.junrunrenli.com"
                ,"-H","platform-type: 2"
                ,"-H","referer: https://rpo2.junrunrenli.com/"
                ,"-H","sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\""
                ,"-H","sec-ch-ua-mobile: ?0"
                ,"-H","sec-ch-ua-platform: \"Windows\""
                ,"-H","sec-fetch-dest: empty"
                ,"-H","sec-fetch-mode: cors"
                ,"-H","sec-fetch-site: same-site"
                ,"-H","tenantcode: SSO"
                ,"-H","user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36"
        }

                ;

        log.info("-----start-----");
        System.out.println(execCurl(cmds));
        log.info("-----end------");
    }


}


